package gui.overview;

import gui.addrecipe.Stars;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.io.File;
import java.io.IOException;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

import org.jdom.JDOMException;

import start.Config;
import start.Start;

import xml.XMLRecipeReader;

/**
 * Vorschau-Panel fuer Rezepte.
 * Zeigt eine Vorschau des in der OverviewTable gewaehlten Rezepts an.
 * @author Dennis Wittkoetter
 * 
 */
public class OverviewPanel_Preview extends JPanel {
	/**ID zur Serialisierung*/
	private static final long serialVersionUID = 1L;
	/**XML-Reader fuer Rezepte*/
	private static XMLRecipeReader xmlreader;
	/**Label fuer zugeordnete Kategorie*/
	private static JLabel lblCategory;
	/**Label fuer zugeordnete Personenzahl*/
	private static JLabel lblPersAmount;
	/**Label fuer zugeordnete Zutaten*/
	private static JLabel lblZutaten;
	/**Sterne fuer Bewertung*/
	private static Stars imgStars;
	/**JScrollPane fuer Zutatenliste*/
	private static JScrollPane scrollPaneIng;
	/**Label fuer zugeordnete Tags*/
	private static JLabel lblTagsAB;
	/**Label fuer Vorschaubild*/
	private static JLabel lblPicture;
	
	
	/**
	 * Konstruiert die Rezeptvorschau fuer das OverviewPanel
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public OverviewPanel_Preview(int selRow) throws JDOMException, IOException {
		xmlreader = new XMLRecipeReader(Start.getRezeptexml()); 
		
		JList ingList = null;
		int recipeID = -1;
		String txt_lblPicture 		= "";
		String txt_lblCategory 		= "";
		String txt_lblPersAmount 	= "";
		String txt_lblIngridients 	= "";
		String txt_lblTags 			= "";
		
		if (selRow != -1) {
			recipeID = Integer.parseInt(OverviewPanel.getTable().getValueAt(selRow, 4).toString());
			
			if (Config.debugMode()) {
				System.out.println("ID: "+recipeID);
				System.out.println("gewählte Zeile: "+selRow);
			}
			
			DefaultListModel model = new DefaultListModel();
			
			for (String elem : xmlreader.getIngredients(recipeID)) {
				System.out.println("Zutat "+elem);
				model.addElement(elem);
			}
			ingList = new JList(model);
			
			txt_lblIngridients 	= "Zutaten:";
			txt_lblPicture 		= xmlreader.getImageName(recipeID);
			//Bugfix
			if (!new File(Config.getBildpfad()+txt_lblPicture).exists())
				txt_lblPicture = "";
				
			txt_lblCategory 	= "Kategorie: "+xmlreader.getCategory1(recipeID);
			txt_lblPersAmount 	= "Für Personen: "+xmlreader.getPersAmount(recipeID);
			txt_lblTags 		= "Tags: "+xmlreader.getTags(recipeID);
			if (txt_lblTags.equals("Tags: "))	txt_lblTags += "keine";
		}
		
		/**Layout einstellen*/
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{63, 135, 63, 0};
		gridBagLayout.rowHeights = new int[]{160, 25, 30, 30, 30, 30, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		/** Label fuers Vorschaubild erstellen */
		if (selRow != -1) {
			if (txt_lblPicture.isEmpty())
				lblPicture = new JLabel(new ImageIcon(Start.getImgs().getKeinBild()));
			else
				lblPicture = new JLabel(new ImageIcon(Config.getBildpfad()+txt_lblPicture));
			lblPicture.setBorder(new LineBorder(new Color(0, 0, 0)));
			GridBagConstraints gbc_lblPicture = new GridBagConstraints();
			gbc_lblPicture.anchor = GridBagConstraints.NORTH;
			gbc_lblPicture.insets = new Insets(5, 0, 5, 5);
			gbc_lblPicture.gridwidth = 7;
			gbc_lblPicture.gridx = 0;
			gbc_lblPicture.gridy = 0;
			add(lblPicture, gbc_lblPicture);
		}
		
		/** Sterne-Bewertung */
		if (selRow != -1) {
			imgStars = new Stars(xmlreader.getRating(recipeID));
			GridBagConstraints gbc_imgStars = new GridBagConstraints();
			gbc_imgStars.anchor = GridBagConstraints.NORTH;
			gbc_imgStars.insets = new Insets(0, 0, 5, 1);
			gbc_imgStars.gridx = 1;
			gbc_imgStars.gridy = 1;
			add(imgStars, gbc_imgStars);
		}
		
		/** Label fuer Kategorie */
		lblCategory = new JLabel(txt_lblCategory);
		GridBagConstraints gbc_lblKategorieSoundso = new GridBagConstraints();
		gbc_lblKategorieSoundso.insets = new Insets(0, 5, 5, 5);
		gbc_lblKategorieSoundso.anchor = GridBagConstraints.SOUTHWEST;
		gbc_lblKategorieSoundso.gridwidth = 7;
		gbc_lblKategorieSoundso.gridx = 0;
		gbc_lblKategorieSoundso.gridy = 2;
		add(lblCategory, gbc_lblKategorieSoundso);
		
		/** Label fuer Personenanzahls */
		lblPersAmount = new JLabel(txt_lblPersAmount);
		GridBagConstraints gbc_lblFrPersonenX = new GridBagConstraints();
		gbc_lblFrPersonenX.insets = new Insets(0, 5, 5, 5);
		gbc_lblFrPersonenX.anchor = GridBagConstraints.WEST;
		gbc_lblFrPersonenX.gridwidth = 7;
		gbc_lblFrPersonenX.gridx = 0;
		gbc_lblFrPersonenX.gridy = 3;
		add(lblPersAmount, gbc_lblFrPersonenX);
		
		/**Label "Zutaten"*/
		lblZutaten = new JLabel(txt_lblIngridients);
		GridBagConstraints gbc_lblZutaten = new GridBagConstraints();
		gbc_lblZutaten.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblZutaten.insets = new Insets(0, 5, 5, 5);
		gbc_lblZutaten.gridx = 0;
		gbc_lblZutaten.gridy = 4;
		add(lblZutaten, gbc_lblZutaten);
		
		if (selRow != -1) {
			/**Scrollpanel zur Kapselung der Zutatenliste*/
			scrollPaneIng = new JScrollPane(ingList);
			GridBagConstraints gbc_scrollPane = new GridBagConstraints();
			gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
			gbc_scrollPane.gridwidth = 6;
			gbc_scrollPane.fill = GridBagConstraints.BOTH;
			gbc_scrollPane.gridx = 1;
			gbc_scrollPane.gridy = 4;
			add(scrollPaneIng, gbc_scrollPane);
		}
		
		/** Label "Tags" */
		lblTagsAB = new JLabel(txt_lblTags);
		GridBagConstraints gbc_lblTagsAB = new GridBagConstraints();
		gbc_lblTagsAB.anchor = GridBagConstraints.WEST;
		gbc_lblTagsAB.gridwidth = 7;
		gbc_lblTagsAB.insets = new Insets(0, 5, 0, 5);
		gbc_lblTagsAB.gridx = 0;
		gbc_lblTagsAB.gridy = 5;
		add(lblTagsAB, gbc_lblTagsAB);

	}

}
